<?php
include_once('./config/config.php');

// 获取员工id
$id = $_GET['id'];
// 查询该id的用户名
$sql = "select p.*,d.name as dname,j.name as jname from `pre_person` as p left join `pre_department` as d on p.dep_id=d.id left join `pre_job` as j on p.job_id=j.id where p.id= '$id'";
// 执行
$person = find($sql);
// 查询部门数据
$depSql = "select * from `pre_department`";
$depData = all($depSql);

if ($_POST) {
  $params = $_POST;
  // 封装数据
  $data = [
    'dep_id' => $params['depid'],
    'job_id' => $params['jobid'],
    'sex' => $params['sex'],
  ];

  if ($person['name'] != $params['name']) {
    if(empty($params['name'])){
      ShowMsg('请输入员工姓名');
    }
    $data['name'] = $params['name'];
  }

  if ($person['mobile'] != $params['mobile']) {
    if(empty($params['mobile'])){
      ShowMsg('请输入员工手机号');
    }
    $data['mobile'] = $params['mobile'];
  }

  if ($person['email'] != $params['email']) {
    if(empty($params['email'])){
      ShowMsg('请输入员工邮箱');
    }
    $data['email'] = $params['email'];
  }

  if ($person['address'] != $params['address']) {
    if(empty($params['address'])){
      ShowMsg('请输入员工地址');
    }
    $data['address'] = $params['address'];
  }

  if (empty($params['depid']) || $params['depid'] == 0) {
    ShowMsg('请选择部门');
  }

  if (empty($params['jobid']) || $params['jobid'] == 0) {
    ShowMsg('请选择职位');
  }

  // 判断是否有图片上传
  if (!empty($_FILES['avatar']) && $_FILES['avatar']['error'] === 0) {
    // 调用上传图片函数
    $avatar = upload('avatar');
    if ($avtar['code'] === 0) {
      ShowMsg($avtar['msg']);
      exit;
    }
    // 封装到$data数组里
    $data['avatar'] = $avatar['data'];
  }

  $result = update('person', $data, "`id` = '$id'");

  if ($result['code'] === 1) {
    // $_FILES['avatar']['size'] > 0  上传的图片大小大于0
    if (!empty($data['avatar']) && $_FILES['avatar']['size'] > 0) {
      is_file($person['avatar']) && @unlink($person['avatar']);
    }
    ShowMsg('修改成功', 'PersonList.php');
  } else {
    if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
      is_file($data['avatar']) && @unlink($data['avatar']);
    }
    ShowMsg($result['msg']);
  }
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入公共样式 -->
  <?php include_once('./common/meta.php'); ?>

  <!-- 引入公共js -->
  <?php include_once('./common/script.php'); ?>
</head>
<style>
  .sex {
    display: flex;
    margin-bottom: 10px;
  }

  .sex input,
  .sex label {
    margin: 0px 2px 0px 0px;
  }

  .sex label {
    margin-right: 10px;
  }

  #img {
    width: 150px;
  }

  label[for="avatar"] {
    width: 150px;
  }
</style>

<body>
  <!-- 引入头部 -->
  <?php include_once('./common/header.php'); ?>

  <!-- 引入侧边菜单 -->
  <?php include_once('./common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">编辑员工</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">Index</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='PersonList.php'"><i class="icon-list"></i> 员工列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>姓名</label>
                <input type="text" name="name" class="input-xxlarge" value="<?php echo $person['name'] ?>">
                <label>性别</label>
                <div class="sex">
                  <input type="radio" name="sex" id="man" value="0" <?php echo $person['sex']==0? "checked" : ""?>>
                  <label>保密</label>
                  <input type="radio" name="sex" id="man" value="1" <?php echo $person['sex']==1? "checked" : ""?>>
                  <label>男</label>
                  <input type="radio" name="sex" id="man" value="2" <?php echo $person['sex']==2? "checked" : ""?>>
                  <label>女</label>
                </div>
                <label>手机号</label>
                <input type="tel" name="mobile" class="input-xxlarge" value="<?php echo $person['mobile'] ?>">
                <label>Email</label>
                <input type="email" name="email" class="input-xxlarge" value="<?php echo $person['email'] ?>">
                <label>地址</label>
                <input type="text" name="address" class="input-xxlarge" value="<?php echo $person['address'] ?>">
                <!-- 部门 -->
                <label>所属部门</label>
                <select name="depid" class="input-xlarge">
                  <option value="<?php echo $person['dep_id'] ?>"><?php echo $person['dname'] ?></option>
                  <?php foreach ($depData as $item) { ?>
                    <option value="<?php echo $item['id']; ?>"><?php echo $item['name']; ?></option>
                  <?php } ?>
                </select>
                <!-- 职位 -->
                <label>所在职位</label>
                <select name="jobid" class="input-xlarge">
                  <option value="<?php echo $person['job_id'] ?>"><?php echo $person['jname'] ?></option>
                </select>
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden>
                <label for="avatar">
                  <?php if ($person['avatar']) { ?>
                    <img src="<?php echo $person['avatar']; ?>" alt="" id="img">
                  <?php } else { ?>
                    <img src="/assets/images/upload.png" alt="" id="img">
                  <?php } ?>
                </label>
                <input class="btn btn-primary" type="submit" value="修改" />
              </form>
            </div>
          </div>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Delete Confirmation</h3>
          </div>
          <div class="modal-body">

            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?
            </p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php'); ?>
      </div>
    </div>
  </div>

</body>

</html>

<script>
  $('#avatar').change(function() {
    var file = $(this)[0].files[0]

    var url = GetObjectURL(file)

    $('#img').attr('src', url)
  })
  // 监听部门的值是否有变化
  $('select[name="depid"]').change(function() {
    var id = $(this).val();
    // 发起ajax请求
    $.ajax({
      type: 'post',
      url: 'proces.php',
      dataType: 'json',
      data: {
        action: 'JobSelect',
        id: id,
      },
      success: function(data) {
        var option = '';
        if (data.code === 1) {
          data.data.forEach(item => {
            option += `<option value="${item.id}">${item.name}</option>`
          })
          $('select[name="jobid"]').html(option)
        } else {
          option = `<option>${data.msg}</option>`
          $('select[name="jobid"]').html(option)
        }
      }
    })
  })
</script>